<?php

namespace App\Services;
use APP\Libraries\Tool;
use Illuminate\Support\Facades\DB;
class SupplierService
{
    public function get_my_order($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['after_sale']=explode('/',$arr_page[$a]->after_sale);
                $array[$a]['business']=$Tool->index_field('shopers','ic',$arr_page[$a]->supply_shoper_ic,'title');
                $array[$a]['business_ic']=$arr_page[$a]->supply_shoper_ic;
                $array[$a]['classifyIc']=explode('/',$arr_page[$a]->goods_classify_ic);
                for($b=0;$b<count($array[$a]['classifyIc']);$b++){
                    $array[$a]['classifyName'][$b]=$Tool->index_field('goods_classifys','ic',$array[$a]['classifyIc'][$b],'name');
                }
                $array[$a]['cover_image']=explode('-',$arr_page[$a]->master_graph);
                $array[$a]['describe']=$arr_page[$a]->describe;
                $array[$a]['good_identifier']=$arr_page[$a]->ic;
                $array[$a]['good_number']=$arr_page[$a]->item_number;
                $array[$a]['name']=$arr_page[$a]->name;
                $array[$a]['show_state']=$arr_page[$a]->upper_shelf;
                $array[$a]['textarea']=$arr_page[$a]->details;
                $array[$a]['label']=explode('/',$arr_page[$a]->recommend);
                $array[$a]['createat']=$Tool->datas(2,$arr_page[$a]->created_at);

                $shopers_goods_specs_list=$Tool->index_list('shopers_goods_specs','shopers_goods_ic','=',$arr_page[$a]->ic,'created_at');//规格参数
                for($c=0;$c<count($shopers_goods_specs_list);$c++){
                    $array[$a]['specifications_arr'][$c]['ic']=$shopers_goods_specs_list[$c]->ic;
                    $array[$a]['specifications_arr'][$c]['code']=$shopers_goods_specs_list[$c]->item_number;
                    $array[$a]['specifications_arr'][$c]['factory_price']=$shopers_goods_specs_list[$c]->factory_price;
                    $array[$a]['specifications_arr'][$c]['images']=explode('-',$shopers_goods_specs_list[$c]->picture);
                    $array[$a]['specifications_arr'][$c]['name']=$shopers_goods_specs_list[$c]->name;
                    $array[$a]['specifications_arr'][$c]['retail_price']=$shopers_goods_specs_list[$c]->retail_price;
                    $array[$a]['specifications_arr'][$c]['stock']=$shopers_goods_specs_list[$c]->stock;
                    $array[$a]['specifications_arr'][$c]['supply_price']=$shopers_goods_specs_list[$c]->supply_price;
                }
            }
        }
        return $array;
    }

    public function deliver_order($key,$arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $order_line=[];
                if($key==1){//待发货
                    $order_details=$Tool->index_many_list('order_details','order','=',$arr_page[$a]->order,'express_state','=',1,'shortage_data','=','1','created_at');
                }elseif ($key==2){//已发货
                    $order_details=$Tool->index_many_list('order_details','order','=',$arr_page[$a]->order,'express_state','=',2,'shortage_data','=','1','created_at');
                }elseif ($key==3){//缺货补发
                    $order_details=$Tool->index_many_list('order_details','order','=',$arr_page[$a]->order,'express_state','=',1,'shortage_data','=','2','created_at');
                }
                if($order_details!=[]){
                    $array[$a]['ic']=$arr_page[$a]->ic;
                    $array[$a]['in_orderNum']=$arr_page[$a]->ic;
                    $array[$a]['orderNum']=$arr_page[$a]->order;
                    $array[$a]['order_state']=$arr_page[$a]->order_state;//1待发货 2缺货 3售后
                    $array[$a]['serial']=$arr_page[$a]->ic;//发货流水号
                    $array[$a]['createat']=$Tool->datas(2,$arr_page[$a]->created_at);//订单时间
                    $array[$a]['deliver_time']=$arr_page[$a]->delivery_data;//发货时间
                    $order_line[]=['label'=>'创建订单','time'=>$arr_page[$a]->created_at];
                    //订单图标情况
                    if($arr_page[$a]->payment_state==2){//预付状态时间
                        $order_line[]=['label'=>'已预付','time'=>$arr_page[$a]->payment_data];
                    }
                    if($arr_page[$a]->after_sales_state==2){//售后状态时间
                        $order_line[]=['label'=>'售后','time'=>$arr_page[$a]->after_sales_data];
                    }
                    if($arr_page[$a]->complete_state==2){//完成状态时间
                        $order_line[]=['label'=>'已完成','time'=>$arr_page[$a]->complete_data];
                    }
                    $array[$a]['order_box']['order_line']=$order_line;
                    $good=array();
                    for($b=0;$b<count($order_details);$b++){
                        $good[$b]['o_d_ic']=$order_details[$b]->ic;
                        if($order_details[$b]->express_name){
                            $good[$b]['express_name']=$order_details[$b]->express_name;//物流名称
                            $good[$b]['express_order']=$order_details[$b]->express_order;//物流编号
                        }else{
                            $good[$b]['express_name']='';//物流名称
                            $good[$b]['express_order']='';//物流编号
                        }
                        $good[$b]['factory_price']=$order_details[$b]->univalent;//出厂价格
                        $goods=$Tool->index_first('shopers_goods','ic',$order_details[$b]->shopers_goods_ic);//商品
                        $specs=$Tool->index_first('shopers_goods_specs','ic',$order_details[$b]->shopers_goods_specs_ic);//规格
                        $good[$b]['goodsName']=$goods->name;//商品名称
                        $good[$b]['goodNum']=$goods->ic;//商品编号
                        $good[$b]['num']=$order_details[$b]->number;//购买数量
                        $good[$b]['prepaid_price']=$order_details[$b]->subtotal;//结算价格
                        $good[$b]['specifications_name']=$specs->name;//规格名称
                        $good[$b]['typeNum']=$specs->ic;//规格名称
                    }
                    $array[$a]['order_box']['order_messages']=$good;
                    //物流信息
                    $array[$a]['order_box']['order_user']['address']=$arr_page[$a]->place.'  '.$arr_page[$a]->road;
                    $array[$a]['order_box']['order_user']['name']=$arr_page[$a]->name;
                    $array[$a]['order_box']['order_user']['remarks']=$arr_page[$a]->remarks;
                    $array[$a]['order_box']['order_user']['tel']=$arr_page[$a]->phone;
                }
            }
        }
        return $array;
    }

    public function shipped_export($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['orderNum']=$arr_page[$a]->order;
                $array[$a]['order_user']=[$arr_page[$a]->name,$arr_page[$a]->phone,$arr_page[$a]->place.$arr_page[$a]->road];
                $array[$a]['remarks']=$arr_page[$a]->remarks;
                $order_details=$Tool->index_many_list('order_details','order','=',$arr_page[$a]->order,'express_state','=',1,'shortage_data','=','1','created_at');
                for($b=0;$b<count($order_details);$b++){
                    $array[$a]['express_name']='';//物流名称
                    $array[$a]['express_order']='';//物流编号
                    $goods=$Tool->index_first('shopers_goods','ic',$order_details[$b]->shopers_goods_ic);//商品
                    $specs=$Tool->index_first('shopers_goods_specs','ic',$order_details[$b]->shopers_goods_specs_ic);//规格
                    $array[$a]['goodsName']=$goods->name;//商品名称
                    $array[$a]['goodsNum']=$goods->ic;//商品编号
                    $array[$a]['num']=$order_details[$b]->number;//购买数量
                    $array[$a]['specificationsIc']=$order_details[$b]->ic;//子订单ic
                    $array[$a]['specificationsName']=$specs->name;//规格名称
                    $array[$a]['specificationsNum']=$specs->item_number;//规格编号
                }
            }
        }
        return $array;
    }

    public function logistics_record($arr_page)
    {
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['express_name']=$arr_page[$a]->express_name;//物流名称
                $array[$a]['express_order']=$arr_page[$a]->express_order;//物流编号
                $array[$a]['orderNum']=$arr_page[$a]->order;//订单号
                $array[$a]['serial']=$arr_page[$a]->ic;//流水号
                $array[$a]['time_delivery']=$arr_page[$a]->time;//发货时间
            }
        }
        return $array;
    }

    public function order_record($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $order_line=[];
                $order_details=$Tool->index_list('order_details','order','=',$arr_page[$a]->order,'created_at');
                if($order_details!=[]){
                    $array[$a]['ic']=$arr_page[$a]->ic;
                    $array[$a]['in_orderNum']=$arr_page[$a]->ic;
                    $array[$a]['orderNum']=$arr_page[$a]->order;
                    $array[$a]['order_state']=$arr_page[$a]->order_state;
                    $array[$a]['total_factory']=$arr_page[$a]->prepaid_money;
                    $array[$a]['time_prepaid']=$arr_page[$a]->payment_data;
                    $array[$a]['completion_time']=$arr_page[$a]->complete_data;
                    $order_line[]=['label'=>'创建订单','time'=>$Tool->datas(2,$arr_page[$a]->created_at)];
                    //订单图标情况
                    if($arr_page[$a]->payment_state==2){//预付状态时间
                        $order_line[]=['label'=>'已预付','time'=>$arr_page[$a]->payment_data];
                    }
                    if($arr_page[$a]->after_sales_state==2){//售后状态时间
                        $order_line[]=['label'=>'售后','time'=>$arr_page[$a]->after_sales_data];
                    }
                    if($arr_page[$a]->complete_state==2){//完成状态时间
                        $order_line[]=['label'=>'已完成','time'=>$arr_page[$a]->complete_data];
                    }
                    $array[$a]['order_box']['order_line']=$order_line;
                    for($b=0;$b<count($order_details);$b++){
                        $good[$b]['o_d_ic']=$order_details[$b]->ic;
                        $good[$b]['express_name']=$order_details[$b]->express_name;//物流名称
                        $good[$b]['express_order']=$order_details[$b]->express_order;//物流编号
                        $goods=$Tool->index_first('shopers_goods','ic',$order_details[$b]->shopers_goods_ic);//商品
                        $specs=$Tool->index_first('shopers_goods_specs','ic',$order_details[$b]->shopers_goods_specs_ic);//规格
                        $good[$b]['goodsName']=$goods->name;//商品名称
                        $good[$b]['goodNum']=$goods->ic;//商品编号
                        $good[$b]['num']=$order_details[$b]->number;//购买数量
                        $good[$b]['prepaid_price']=$specs->supply_price;//结算价格
                        $good[$b]['factory_price']=$specs->factory_price;//出厂价格
                        $good[$b]['specifications_name']=$specs->name;//规格名称
                        $good[$b]['typeNum']=$specs->ic;//规格名称
                    }
                    $array[$a]['order_box']['order_messages']=$good;
                    //物流信息
                    $array[$a]['order_box']['order_user']['address']=$arr_page[$a]->place.'  '.$arr_page[$a]->road;
                    $array[$a]['order_box']['order_user']['name']=$arr_page[$a]->name;
                    $array[$a]['order_box']['order_user']['remarks']=$arr_page[$a]->remarks;
                    $array[$a]['order_box']['order_user']['tel']=$arr_page[$a]->phone;
                }
            }
        }
        return $array;
    }

    public function supplyB_business()
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $shoper=$Tool->index_first('shopers','ic',$s_ic);
        $array['ic']=$shoper->ic;
        $array['address']=$shoper->lx_place;
        $array['code']=$shoper->zj_credit_code;
        $array['contacts']=$shoper->lx_contacts;
        $array['name']=$shoper->title;
        $array['registere']=$shoper->zj_place;
        $array['tel']=$shoper->lx_mobile;
        $array['term']=explode('-',$shoper->zj_term);
        return $array;
    }

    public function get_system_messages($name,$group,$system,$content)
    {
        $Tool = new Tool();
        $date =$Tool->datas(1,'');
        $array['ic']=getfirstic();
        $array['state'] = 1;
        $array['created_at'] = $date;
        $array['time'] = time()*1000;
        $array['group'] = $group;
        $array['type'] = $system;
        $array['content'] = $content;
        $array['name'] = $name;
        return $Tool->mysql_insert('notices',$array);
    }

}